package com.example.dragdemo;
import java.io.File;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;


public class GridAdapter extends BaseAdapter{

	private LayoutInflater mInflater;
	private Context mContext;
	private List<File> mFiles;
	
	public GridAdapter(Context context, List<File> files) {
		super();
		this.mInflater = LayoutInflater.from(context);
		this.mContext = context;
		mFiles = files;
	}
	
	public void setDatas(List<File> files){
		mFiles = files;
	}

	public List<File> getList(){
		return mFiles;
	}

	@Override
	public int getCount() {
		return mFiles.size();
	}

	@Override
	public File getItem(int position) {
		return mFiles.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ItemHandler mItemHandler;
		if(convertView == null){
			mItemHandler = new ItemHandler();
			convertView = mInflater.inflate(R.layout.grid_item,null);
			mItemHandler.mImage = (ImageView) convertView.findViewById(R.id.image);
			mItemHandler.mText = (TextView) convertView.findViewById(R.id.content);
			AbsListView.LayoutParams param = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,250);
	        convertView.setLayoutParams(param);
			convertView.setTag(mItemHandler);
		}else{
			mItemHandler = (ItemHandler) convertView.getTag();
		}
		File file = mFiles.get(position);
		if(file.isDirectory()){
			mItemHandler.mImage.setImageResource(R.drawable.ico_foder);
		}else{
			mItemHandler.mImage.setImageResource(R.drawable.ico_text);
		}
		mItemHandler.mText.setText(file.getName());
		return convertView;
	}
}

class ItemHandler{
	public ImageView mImage;
	public TextView mText;
}
